package com.cskaoyan.market.controller;

import com.cskaoyan.market.db.domain.MarketAd;
import com.cskaoyan.market.db.domain.MarketCoupon;
import com.cskaoyan.market.service.AdminCouponService;
import com.cskaoyan.market.service.impl.AdminCouponServiceImpl;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/admin/coupon/*")
public class AdminCouponController extends HttpServlet {
    AdminCouponService adminCouponService=new AdminCouponServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/coupon/", "");
        if("list".equals(op)){
            list(req, resp);
        }
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");
        String name = req.getParameter("name");
        Short type = Short.valueOf(req.getParameter("type"));
        Short status = Short.valueOf(req.getParameter("status"));
        List<MarketCoupon> couponList = adminCouponService.list(page, limit, sort, order,name, type,status);
        //对于分页查询的这些接口，那么我们需要调用oKlIST方法
        Object o = ResponseUtil.okList(couponList);
        resp.getWriter().println(JacksonUtil.writeValueAsString(o));
    }
}
